﻿using System;
using System.Threading.Tasks;
using System.Windows.Forms;
using EasyBackup.Utils;

namespace EasyBackup.Forms
{
    public partial class AppForm : Form
    {                
        public AppForm()
        {            
            InitializeComponent();                                                    
        }        

        private void AppFormLoad(object sender, EventArgs e)
        {
            InitForm();
            CheckService();
        }

        private void InitForm()
        {
            notifyIcon.Visible = true;
            Hide();

            appDirTextBox.Text = App.Location;
            logDirTextBox.Text = App.LogDir;
            scheduleTextBox.Text = App.SchedulePath;
        }

        private void CheckService()
        {
            Task.Factory.StartNew(() => BackupService.Start());
        }

        private void HideOrShowSettings()
        {
            if (this.Visible == false)
            {
                this.Show();
                this.WindowState = FormWindowState.Normal;
                this.CenterToScreen();
            }
            else
            {
                this.Hide();
            }
        }
              
        private void ExitMenuItemClick(object sender, EventArgs e)
        {
            Application.Exit();
        }        

        private void SettingsMenuItemClick(object sender, EventArgs e)
        {
            HideOrShowSettings();
        }        

        private void NotifyIconMouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                HideOrShowSettings();
            }            
        }        

        private void AppFormFormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                HideOrShowSettings();
            }              
        }

        private void AppLabelClick(object sender, EventArgs e)
        {
            Explorer.Open(appDirTextBox.Text);            
        }

        private void LogLabelClick(object sender, EventArgs e)
        {
            Explorer.Open(logDirTextBox.Text);            
        }

        private void ScheduleLabelClick(object sender, EventArgs e)
        {
            Explorer.Open(scheduleTextBox.Text);            
        }

        private void RestartServiceClick(object sender, EventArgs e)
        {
            Task.Run(() => BackupService.Restart());  
        }                       
    }
}
